package com.jt.controller;

import com.jt.entity.Page;
import com.google.gson.Gson;
import com.jt.entity.Attendance;
import com.jt.entity.Student;
import com.jt.entity.Teacher;
import com.jt.service.AttendanceService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    @RequestMapping("/add")
    public String  add(Attendance attendance,Integer stu_id,Integer t_id){
        Student student = new Student();
        student.setStuid(stu_id);
        attendance.setStudent(student);
        Teacher teacher = new Teacher();
        teacher.setTid(t_id);
        attendance.setTeacher(teacher);
        Integer add = attendanceService.add(attendance);
        if(add!=null){
            System.out.println(attendance);
        }
        return "attendance/attendance_add";
    }


    @RequestMapping("/queryPage")
    public String query(ModelMap modelMap,   HttpServletRequest request, Page page){
        String currentPage=request.getParameter("currentPage");
        System.out.println(currentPage);
        System.out.println(page.getCurrentPage());
        Integer curr=1;
        if (currentPage!=null){
            curr=Integer.parseInt(currentPage);
            System.out.println(curr);
        }
        page.setCurrentPage(curr);
        page.setCount(attendanceService.count());
        page.setUrl("/attendance/queryPage?");
       RowBounds rowBound=new RowBounds((page.getCurrentPage()-1)*page.getSize(),page.getSize());
        List<Attendance> attendances = attendanceService.querypage(rowBound);
        System.out.println(attendances);
        System.out.println(page);
        modelMap.addAttribute("attendances",attendances);
        modelMap.addAttribute("page",page);
        return "attendance/attendance_list";
    }


    @RequestMapping("/queryById")
    public String queryById(ModelMap modelMap,Integer id){
        Attendance attendance = attendanceService.queryById(id);
        System.out.println(attendance);
        modelMap.addAttribute("attendance",attendance);
        return "attendance/attendance_update";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        Integer delete = attendanceService.delete(id);
        if(delete!=null){
            System.out.println("删除"+delete);
        }
        return "attendance/attendance_list";
    }

    @RequestMapping("/ajaxQuery")
    public void ajaxQuery(HttpServletResponse response){
        List<Attendance> query = attendanceService.query();
           Gson gson = new Gson();
           String s = gson.toJson(query);
          System.out.println(s);
         try {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
}

     @RequestMapping("/queryInfo")
     public String queryInfo(ModelMap modelMap, String info,HttpServletResponse response){
         response.setContentType("text/html;charset=utf-8");
         List<Attendance> attendances = attendanceService.queryInfo(info);
         System.out.println(attendances);
         modelMap.addAttribute("attendances",attendances);
         return "attendance/attendance_list";
     }


    @RequestMapping("/update")
    public String update(Attendance attendance,Integer stu_id,Integer t_id){
        Student student = new Student();
        student.setStuid(stu_id);
        attendance.setStudent(student);
        Teacher teacher = new Teacher();
        teacher.setTid(t_id);
        attendance.setTeacher(teacher);
        Integer update = attendanceService.update(attendance);
        System.out.println(attendance);
        System.out.println("update"+update);
        return "attendance/attendance_list";
    }
}
